package com.weixin1.servlet;

import com.weixin1.bean.Employee;
import com.weixin1.dto.R;
import com.weixin1.service.EmployeeService;
import com.weixin1.utils.JSONUtil;
import com.weixin1.utils.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/employeeservlet/*")
public class EmployeeServlet extends HttpServlet {
    EmployeeService es=new EmployeeService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String PathInfo = req.getPathInfo().substring(1);
        if("list".equals(PathInfo)){
            list(req,resp);
        }else if("removeById".equals(PathInfo)){
            removeById(req,resp);
        }else if("save".equals(PathInfo)){
            save(req,resp);
        }else if("update".equals(PathInfo)){
            update(req,resp);
        }
    }

    //修改数据
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
//            long eId=Long.valueOf(req.getParameter("eId"));
//            String eName = req.getParameter("eName");
//            String eGender = req.getParameter("eGender");
//            int eAge = Integer.valueOf(req.getParameter("eAge"));
//            String eBirthday=req.getParameter("eBirthday");
//            String eJob = req.getParameter("eJob");
//            String eLocation = req.getParameter("eLocation");
//
//            Employee employee=new Employee();
//            employee.setEId(eId);
//            employee.setEName(eName);
//            employee.setEGender(eGender);
//            employee.setEAge(eAge);
//            employee.setEBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(eBirthday));
//            employee.setEJob(eJob);
//            employee.setELocation(eLocation);
//            System.out.println(employee.toString());
            Employee employee = MyBeanUtils.reqToBean(req, Employee.class);
            boolean b = es.update(employee);
            System.out.println(b);
            if(b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else{
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //添加数据
    private void save(HttpServletRequest req, HttpServletResponse resp) {
//        String eName = req.getParameter("eName");
//        String eGender = req.getParameter("eGender");
//        Integer eAge = Integer.valueOf(req.getParameter("eAge"));
//        String eBirthday=req.getParameter("eBirthday");
//        String eJob = req.getParameter("eJob");
//        String eLocation = req.getParameter("eLocation");
//
//        Employee employee=new Employee();
        try {
//            employee.setEName(eName);
////            employee.setEGender(eGender);
////            employee.setEAge(eAge);
////            employee.setEBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(eBirthday));
////            employee.setEJob(eJob);
////            employee.setELocation(eLocation);
            Employee employee = MyBeanUtils.reqToBean(req, Employee.class);
            boolean save = es.save(employee);
            if(save){
                resp.getWriter().write(JSONUtil.toString(R.ok()));
            }else{
                resp.getWriter().write(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //删除一条数据
    private void removeById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long eId = Long.valueOf(req.getParameter("eId"));
        try {
            boolean b = es.removeById(eId);
            if(b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else{
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //分页查询
    private void list(HttpServletRequest req, HttpServletResponse resp) {

        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));


        try {
            Map<String,Object> map=new HashMap<>();
            List<Employee> employees = es.selectByPage(page, limit);
            int total = es.gettotal();

            map.put("code",0);
            map.put("count",total);
            map.put("data",employees);

            resp.getWriter().print(JSONUtil.toString(map));

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
